package com.rcl.himalaya_kotlin.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/9/3.
 * PS: Not easy to write code, please indicate.
 */
class RoundRectImageView : AppCompatImageView {
    private val roundRatio = 0.1f
    private var path: Path? = null

    constructor(context: Context?) : super(context!!)

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    )

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!, attrs, defStyleAttr
    )

    override fun onDraw(canvas: Canvas) {
        if (path == null) {
            path = Path()
            path!!.addRoundRect(
                RectF(0f, 0f, width.toFloat(), height.toFloat()),
                roundRatio * width,
                roundRatio * height,
                Path.Direction.CW
            )
        }
        canvas.save()
        canvas.clipPath(path!!)
        super.onDraw(canvas)
        canvas.restore()
    }
}